﻿#region Copyright

//  ======================================================
//      Copyright (c) 2011 Toulr All rights reserved.
//     
//      The use and distribution terms for this software are contained in the file
//      named license.txt, which can be found in the root of this distribution.
//      By using this software in any fashion, you are agreeing to be bound by the
//      terms of this license.
//     
//     You must not remove this notice, or any other, from this software.
//  ======================================================

#endregion

#region Using Namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using Toulr.Domains;
using Toulr.Domains.Business;
using Toulr.Models;
using Toulr.Services;
using Toulr.Util.Logging;

#endregion

namespace Toulr.Core.Services
{
    public class BusinessService : ApplicationContextService, IBusinessService
    {
        private static readonly Repository<Promotions> Repository = new Repository<Promotions>();
        private readonly ILogger _logger = LogHelper.LogFactoy.Create("BusinessService");

        #region IBusinessService Members

        public ActionResult<Promotions> CreatePromotions(Promotions promotions)
        {
            try
            {
                Promotions p = Repository.Add(promotions);
                return new ActionResult<Promotions>(0, GetMessageString("MSG_OPERATION_SUCCESSFUL"), p);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message, ex);
                return new ActionResult<Promotions>(1, GetMessageString("ERROR_UNKNOW"), promotions);
            }
        }

        public ActionResult<Promotions> ModifyPromotions(Promotions promotions)
        {
            try
            {
                Promotions p = Repository.Update(promotions);
                return new ActionResult<Promotions>(0, GetMessageString("MSG_OPERATION_SUCCESSFUL"), p);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message, ex);
                return new ActionResult<Promotions>(1, GetMessageString("ERROR_UNKNOW"), promotions);
            }
        }

        public ActionResult<bool> DeletePromotions(int id)
        {
            try
            {
                Promotions p = GetPromotions(id);
                if (p != null)
                    Repository.Delete(p);
                return new ActionResult<bool>(0, GetMessageString("MSG_OPERATION_SUCCESSFUL"), true);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message, ex);
                return new ActionResult<bool>(1, GetMessageString("ERROR_UNKNOW"), false);
            }
        }

        public ActionResult<List<Promotions>> QueryPromotions(Promotions queryCondition, int pageSize, int pageNum)
        {
            IQueryable<Promotions> query=null;
            var result = query.Skip(pageSize * pageNum).Take(pageSize);
            return null;
        }



        #endregion
        /// <summary>
        ///   根据ID获取一个团购信息
        /// </summary>
        public Promotions GetPromotions(int id)
        {
            return Repository.DbContext.Promotionses.First(r => r.Id == id);
        }
    }
}